﻿<%@ WebHandler Language="C#" Class="DoubleBallTool" %>

using System;
using System.Web;
using System.Data;
using System.Collections.Generic;
using System.Web.Script.Serialization;

public class DoubleBallTool : IHttpHandler {


    public void ProcessRequest (HttpContext context) {

        if (ReadRequest.RequestString("KZselect") != "")
        {
            List<Model.LotteryAnnouncement> lstSelectKZ = new List<Model.LotteryAnnouncement>();
            string[] kzSelectAndNum = ReadRequest.RequestString("KZselect").Split(' ');
            string num = kzSelectAndNum[0];
            string[] kzSelect = kzSelectAndNum[1].Split(',');
            int QS = 0;

            for (int s = 0; s < kzSelect.Length; s++)
            {
                if (kzSelect[s] == "") break;
                for (int n = 0; n < 3; n++)
                {
                    Model.LotteryAnnouncement modSelectKZ = new Model.LotteryAnnouncement();

                    if (n == 0) QS = Convert.ToInt32(kzSelect[s]) + 1;
                    else if (n == 1) QS = Convert.ToInt32(kzSelect[s]);
                    else if (n == 2) QS = Convert.ToInt32(kzSelect[s]) - 1;

                    if (QS == 0) return;

                    List<Model.LotteryAnnouncement> lstDBallModel = BLL.LotteryAnnouncement.GetListOrderBy(" ", " LotteryType=1 AND LotteryQS=" + QS + " ", " top " + 1, " [LotteryQS],[LotteryNO], [oneMoney],[twoMoney],[threeMoney],[fourMoney],[fiveMoney],[sixMoney] ");
                    if (lstDBallModel == null || lstDBallModel.Count < 0)
                    {
                        modSelectKZ.LotteryQS = QS.ToString();
                        modSelectKZ.LotteryNo = "未开奖";
                        lstSelectKZ.Add(modSelectKZ);
                        continue;
                    }

                    modSelectKZ.LotteryQS = lstDBallModel[0].LotteryQS;
                    modSelectKZ.LotteryNo = lstDBallModel[0].LotteryNo;

                    if (n == 1)
                    {
                        if (num == "6")
                        {
                            modSelectKZ.sixMoney = lstDBallModel[0].sixMoney;
                        }
                        else if (num == "5")
                        {
                            modSelectKZ.fiveMoney = lstDBallModel[0].fiveMoney;
                        }
                        else if (num == "4")
                        {
                            modSelectKZ.fourMoney = lstDBallModel[0].fourMoney;
                        }
                        else if (num == "3")
                        {
                            modSelectKZ.threeMoney = lstDBallModel[0].threeMoney;
                        }
                        else if (num == "2")
                        {
                            modSelectKZ.twoMoney = lstDBallModel[0].twoMoney;
                        }
                        else if (num == "1")
                        {
                            modSelectKZ.oneMoney = lstDBallModel[0].oneMoney;
                        }
                    }

                    lstSelectKZ.Add(modSelectKZ);
                }
            }
            string bb = fnObjectToJSON(lstSelectKZ);
            context.Response.Write(bb);
        }
        else
        {
            string QSstr = ReadRequest.RequestString("QS");

            string QS = QSstr.Substring(0, QSstr.Length - 5);
            string cookie = ReadRequest.RequestString("changeBall");
            string sub = cookie.Substring(0, cookie.Length - 2);
            string sub1 = sub.Remove(0,2);


            //需要比对的号码
            string[] DoubleCookies =sub1.Split(',');

            //单式投注号码
            string DanShi = "";
            //复式投注号码
            string FuShi = "";

            for (int b = 0; b < DoubleCookies.Length; b++)
            {
                //如果包含$是 复式 否则是单式
                if (DoubleCookies[b].IndexOf("$") > -1)
                {
                    FuShi += DoubleCookies[b] + "|";
                }
                else
                {
                    DanShi += DoubleCookies[b] + "|";
                }
            }

            //string[] DanShiBall = FuShi.Split('|');
            //string[] FuShiBall = FuShi.Split('|');

            List<Model.LotteryAnnouncement> lstDBallModel = BLL.LotteryAnnouncement.GetListOrderBy(" id desc ", " LotteryType=1 ", " top " + QS," [LotteryQS],[LotteryNO], [oneMoney],[twoMoney],[threeMoney],[fourMoney],[fiveMoney],[sixMoney] ");
            if (lstDBallModel.Count < 0 || lstDBallModel == null) return;

            string one = fnObjectToJSON(OneAward(FuShi, DanShi, 6, lstDBallModel));


            context.Response.Write(one);
        }
    }

    public List<Model.LotteryAnnouncement> OneAward(string FuShi,string DanShi,int num, List<Model.LotteryAnnouncement> lstDBallModel)
    {
        List<Model.LotteryAnnouncement> lstOneModel = new List<Model.LotteryAnnouncement>();
        List<Model.DoubleBall> lstDoubleMod = new List<Model.DoubleBall>();
        //lstDoubleMod = GetAllZH(inAllBall);
        lstDoubleMod= AllZH(FuShi, DanShi, 6);

        //所有的球分组
        string[] arrAllBall;
        //所有的红球
        string[] allRed;
        string allBlue;
        //所有中奖号码的变量
        string[] zAllBall;
        string[] zAllRedBall;
        //用来判断是否是当前行
        int CurrentLine = 0;
        //用来记录每条数据 红球 相同的数量
        int[] RedSameNum = new int[lstDBallModel.Count];
        bool CCCC = true;

        for (int i = 0; i < lstDoubleMod.Count;i++)
        {
            CCCC = true;
            arrAllBall = lstDoubleMod[i].Larr.Split('+');
            allRed = arrAllBall[0].Split(',');
            allBlue = arrAllBall[1].Replace("\"","");
            for (int c = 0; c < allRed.Length; c++)
            {

                //所有中奖的号码 中奖号码相关变量命名 前都有 z
                for (int j = 0; j < lstDBallModel.Count; j++)
                {

                    zAllBall = lstDBallModel[j].LotteryNo.Split('+');
                    zAllRedBall = zAllBall[0].Split(',');
                    for (int d = 0; d < zAllRedBall.Length; d++)
                    {
                        if (allRed[c] == zAllRedBall[d])
                        {
                            //如果相等证明是当前行
                            if (CurrentLine == i)
                            {
                                RedSameNum[j] = RedSameNum[j] + 1;
                            }
                            else
                            {
                                for (int a = 0; a < lstDBallModel.Count; a++)
                                {
                                    RedSameNum[a] = 0;
                                }
                                RedSameNum[j] = 1;
                                CurrentLine = i;
                            }

                            break;
                        }

                    }



                }

            }
            for (int j = 0; j < lstDBallModel.Count; j++)
            {
                zAllBall = lstDBallModel[j].LotteryNo.Split('+');
                zAllRedBall = zAllBall[0].Split(',');

                //红球相等数量6+1一等奖
                if (allBlue == zAllBall[1] && RedSameNum[j] == 6)
                {
                    Model.LotteryAnnouncement ModOne = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModOne.id = 1;
                        ModOne.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModOne.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModOne.oneMoney = lstDBallModel[j].oneMoney;

                        lstOneModel.Add(ModOne);
                    }

                }
                else if (allBlue != zAllBall[1] && RedSameNum[j] == 6)//6+0 二等奖
                {
                    Model.LotteryAnnouncement ModTwo = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModTwo.id = 2;
                        ModTwo.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModTwo.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModTwo.twoMoney = lstDBallModel[j].twoMoney;

                        lstOneModel.Add(ModTwo);

                    }




                }
                else if (allBlue == zAllBall[1] && RedSameNum[j] == 5)//5+1三等奖
                {
                    Model.LotteryAnnouncement ModThree = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModThree.id = 3;
                        ModThree.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModThree.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModThree.threeMoney = lstDBallModel[j].threeMoney;

                        lstOneModel.Add(ModThree);

                    }
                }
                else if (allBlue == zAllBall[1] && RedSameNum[j] == 4)//4+1或5四等奖
                {
                    Model.LotteryAnnouncement ModFour = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModFour.id = 4;
                        ModFour.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModFour.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModFour.fourMoney = lstDBallModel[j].fourMoney;

                        lstOneModel.Add(ModFour);

                    }
                }
                else if (allBlue != zAllBall[1] && RedSameNum[j] == 5)//4+1或5四等奖
                {
                    Model.LotteryAnnouncement ModFour = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModFour.id = 4;
                        ModFour.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModFour.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModFour.fourMoney = lstDBallModel[j].fourMoney;

                        lstOneModel.Add(ModFour);
                    }
                }
                else if (allBlue == zAllBall[1] && RedSameNum[j] == 3)//3+1或4五等奖
                {
                    Model.LotteryAnnouncement ModFive = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModFive.id = 5;
                        ModFive.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModFive.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModFive.fiveMoney = lstDBallModel[j].fiveMoney;

                        lstOneModel.Add(ModFive);
                    }
                }
                else if (allBlue != zAllBall[1] && RedSameNum[j] == 4)//3+1或4五等奖
                {
                    Model.LotteryAnnouncement ModFive = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModFive.id = 5;
                        ModFive.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModFive.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModFive.fiveMoney = lstDBallModel[j].fiveMoney;

                        lstOneModel.Add(ModFive);
                    }
                }
                else if (allBlue == zAllBall[1] && RedSameNum[j] == 2)//2+1，1+1或1六等奖
                {
                    Model.LotteryAnnouncement ModSix = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModSix.id = 6;
                        ModSix.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModSix.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModSix.sixMoney = lstDBallModel[j].sixMoney;

                        lstOneModel.Add(ModSix);
                    }
                }
                else if (allBlue == zAllBall[1] && RedSameNum[j] == 1)//2+1，1+1或1六等奖
                {
                    Model.LotteryAnnouncement ModSix = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModSix.id = 6;
                        ModSix.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModSix.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModSix.sixMoney = lstDBallModel[j].sixMoney;

                        lstOneModel.Add(ModSix);
                    }
                }
                else if (allBlue == zAllBall[1] && RedSameNum[j] == 0)//2+1，1+1或1六等奖
                {
                    Model.LotteryAnnouncement ModSix = new Model.LotteryAnnouncement();

                    if (CCCC != false)
                    {
                        ModSix.id = 6;
                        ModSix.LotteryQS = lstDBallModel[j].LotteryQS;
                        ModSix.LotteryNo = lstDBallModel[j].LotteryNo;
                        ModSix.sixMoney = lstDBallModel[j].sixMoney;

                        lstOneModel.Add(ModSix);
                    }
                }
            }

        }

        return lstOneModel;
    }




    /// <summary>
    /// 根据传入的参数，，组合成组合 
    /// </summary>
    /// <param name="DoubleCookies"></param>
    /// <returns></returns>
    public List<Model.DoubleBall> GetAllZH(string[] DoubleCookies)
    {
        //所有可能生成的红球组合
        List<Model.DoubleBall> lstModAllZHball = BLL.DoubleBall.GetListOrderBy("");
        //保存所有跟传进的参数，组合成功的 号码组合
        List<Model.DoubleBall> lstZHCGMod = new List<Model.DoubleBall>();

        string[] AllzhBall;
        //保存1百多万条数据每一条数据和传来的求 相等的个数
        int[] zl = new int[lstModAllZHball.Count];
        int aaaa = 0;
        string ReplaceRedBall = "";
        //保存 已经组成组合的数据，，用来判断是否加入重复的组合
        string ZHCGId = "";
        string[] arrZHCGIdandBlue;
        int bbbb = 0;
        string ReplaceBlueBall = "";
        for (int i = 0; i < DoubleCookies.Length; i++)
        {
            string[] arrBall = DoubleCookies[i].Split(' ');
            //arrBall[1]==篮球
            string[] arrblueBall = arrBall[1].Split('*');
            string[] arrRedBall = arrBall[0].Split('*');

            //遍历传进来的红球  每一组红球【i】
            foreach (string RedBall in arrRedBall)
            {
                ReplaceRedBall = RedBall.Replace("\"","");
                //遍历所有号码的组合表
                for (int k = 0; k < lstModAllZHball.Count; k++)
                {
                    //所有号码的组合表 中每个组合中的求 然后遍历
                    AllzhBall = lstModAllZHball[k].Larr.Split(',');
                    for (int a = 0; a < AllzhBall.Length; a++)
                    {
                        //必须每组 的球相等并且 相等的红球个数==6 才可以组合成功
                        if (ReplaceRedBall == AllzhBall[a] )
                        {
                            if (aaaa != i)
                            {
                                for (int w = 0; w < lstModAllZHball.Count; w++)
                                {
                                    zl[w] = 0;

                                }
                                zl[k] = 1;
                                aaaa = i;
                            }
                            else
                            {
                                //保存所有号码组合每一条号码组合中 与传进的求 相等的数量
                                zl[k] = zl[k] + 1;
                            }


                            //如果 zl[k]  ==6 证明组合成功  并保存到 list 中
                            if (zl[k] == 6)
                            {

                                //蓝球个数遍历每一个蓝球 把蓝球加入每一个成功的组合
                                for (int m = 0; m < arrblueBall.Length; m++)
                                {
                                    Model.DoubleBall ZHCGMod = new Model.DoubleBall();
                                    ZHCGMod.Larr = lstModAllZHball[k].Larr + "+" + arrblueBall[m];

                                    //判断 加入的组合是否重复
                                    if (ZHCGId == "" || ZHCGId == null)
                                    {
                                        //lstZHCGMod.Add(ZHCGMod);
                                        //    ZHCGId += lstModAllZHball[k].LId + "*" + arrblueBall[m] + ",";
                                        bbbb = 0;
                                    }
                                    else
                                    {
                                        arrZHCGIdandBlue = ZHCGId.Split(',');
                                        for (int h = 0; h < arrZHCGIdandBlue.Length; h++)
                                        {
                                            if (arrZHCGIdandBlue[h] == null || arrZHCGIdandBlue[h] == "")
                                            {
                                                continue;
                                            }

                                            string[] arr1 = arrZHCGIdandBlue[h].Split('*');
                                            if (arrblueBall[m] != "")
                                            {
                                                ReplaceBlueBall = arrblueBall[m].Replace("\"", "");
                                            }
                                            else ReplaceBlueBall = arrblueBall[m];
                                            //arr1[0] == DoubleBall的id  ，arr1[1]==篮球
                                            string arr11 = "";
                                            if (arr1[0] == lstModAllZHball[k].LId.ToString())
                                            {

                                                foreach (string redBlue in arr1)
                                                {
                                                    arr11 = arr1[1].Replace("\"", "");
                                                    if (arr11 == ReplaceBlueBall)
                                                    {
                                                        bbbb = 10;
                                                    }
                                                }
                                            }

                                        }
                                    }

                                    if (bbbb != 10)
                                    {
                                        lstZHCGMod.Add(ZHCGMod);
                                        ZHCGId += lstModAllZHball[k].LId + "*" + arrblueBall[m] + ",";
                                    }
                                    bbbb = 0;

                                }
                                aaaa = i;
                            }

                        }
                    }
                }
            }
        }
        return lstZHCGMod;
    }

    /// <summary>
    /// 此方法为单式和复式加起来的组合
    /// </summary>
    /// <param name="Fushi"></param>
    /// <param name="DanShi"></param>
    /// <param name="num"></param>
    /// <returns></returns>
    public List<Model.DoubleBall> AllZH(string Fushi, string DanShi, int num)
    {
        List<Model.DoubleBall> lstZHCGMod = new List<Model.DoubleBall>();
        Data.Combine DataCombine = new Data.Combine();

        //复式用到的变量
        //所有的求包括红球（胆，拖） 篮球
        string AllZHs = "";
        //托胆选号 所有能匹配上的组合
        string matchingZHs = "";
        //统计生成组合中所有 所有匹配的胆码的数量
        int index = 0;
        string[] FuShiBalls = Fushi.Split('|');
        int[] Fu ;
        //复式用到的变量

        //单式用到的变量

        string[] DanShiBalls = DanShi.Split('|');
        int[] Dan;

        //单式用到的变量

        for (int i = 0; i < FuShiBalls.Length; i++)
        {

            if (FuShiBalls[i] == "") break;

            string FuAllBall = FuShiBalls[i].Replace("\"", "");
            string[] AllBall = FuAllBall.Split(' ');
            string[] BlueBall = AllBall[1].Split('*');
            string[] RedBall = AllBall[0].Split('$');
            string[] DT = RedBall[0].Split('*');
            string[] red = RedBall[1].Split('*');
            int count = DT.Length + red.Length;
            int fuCount = 0;
            Fu = new int[count];
            for (int j = 0; j < DT.Length; j++)
            {
                Fu[j] = Convert.ToInt32(DT[j]);
            }
            for (int k = 0; k < red.Length; k++)
            {
                fuCount = DT.Length + k;
                Fu[fuCount] = Convert.ToInt32(red[k]);
            }
            AllZHs = DataCombine.print(DataCombine.combine(Fu, num),num);

            string[] matchingZH = AllZHs.Split('|');
            for (int y = 0; y < matchingZH.Length; y++)
            {
                for (int w = 0; w < DT.Length; w++)
                {
                    if (matchingZH[y].IndexOf(DT[w]) > -1)
                    {
                        index++;
                    }
                }
                if (index == DT.Length)
                {
                    for (int h = 0; h < BlueBall.Length; h++)
                    {
                        Model.DoubleBall ZHCGMod = new Model.DoubleBall();
                        ZHCGMod.Larr = matchingZH[y] + "+" + BlueBall[h];
                        lstZHCGMod.Add(ZHCGMod);
                        //matchingZHs += matchingZH[y] + "+" + BlueBall[h] + " ";
                    }
                }
                index = 0;

            }

        }

        for (int a = 0; a < DanShiBalls.Length; a++)
        {
            if (DanShiBalls[a] == "") break;

            string DanAllBall = DanShiBalls[a].Replace("\"", "");
            string[] AllBall = DanAllBall.Split(' ');
            string[] BlueBall = AllBall[1].Split('*');
            string[] RedBall = AllBall[0].Split('*');
            Dan = new int[RedBall.Length];
            for (int j = 0; j < RedBall.Length; j++)
            {
                Dan[j] = Convert.ToInt32(RedBall[j]);
            }

            AllZHs = DataCombine.print(DataCombine.combine(Dan, num),num);

            string[] matchingZH = AllZHs.Split('|');
            for (int y = 0; y < matchingZH.Length; y++)
            {
                if (matchingZH[y] == "") break;
                for (int h = 0; h < BlueBall.Length; h++)
                {
                    Model.DoubleBall ZHCGMod = new Model.DoubleBall();
                    ZHCGMod.Larr = matchingZH[y] + "+" + BlueBall[h];
                    lstZHCGMod.Add(ZHCGMod);
                    //matchingZHs += matchingZH[y] + "+" + BlueBall[h] + " ";
                }

            }

        }


        //string[] leng = matchingZHs.Split(' ');
        //leng.Length;
        return lstZHCGMod;

    }

    /// <summary>
    /// 此方法为所有复式所有的组合
    /// </summary>
    /// <param name="Fushi"></param>
    /// <param name="DanShi"></param>
    /// <param name="num"></param>
    /// <returns></returns>   
    public string TDZH(string Fushi, string DanShi, int num)
    {

        Data.Combine DataCombine = new Data.Combine();
        //所有的求包括红球（胆，拖） 篮球
        string AllZHs = "";
        //托胆选号 所有能匹配上的组合
        string matchingZHs = "";
        //统计生成组合中所有 所有匹配的胆码的数量
        int index = 0;
        string[] FuShiBalls = Fushi.Split('|');

        int[] Fu ;

        for (int i = 0; i < FuShiBalls.Length; i++)
        {

            if (FuShiBalls[i] == "") break;

            string FuAllBall = FuShiBalls[i].Replace("\"", "");
            string[] AllBall = FuAllBall.Split(' ');
            string[] BlueBall = AllBall[1].Split('*');
            string[] RedBall = AllBall[0].Split('$');
            string[] DT = RedBall[0].Split('*');
            string[] red = RedBall[1].Split('*');
            int count = DT.Length + red.Length;
            int fuCount = 0;
            Fu = new int[count];
            for (int j = 0; j < DT.Length; j++)
            {
                Fu[j] = Convert.ToInt32(DT[j]);
            }
            for (int k = 0; k < red.Length; k++)
            {
                fuCount = DT.Length + k;
                Fu[fuCount] = Convert.ToInt32(red[k]);
            }
            AllZHs = DataCombine.print(DataCombine.combine(Fu, num),num);

            string[] matchingZH = AllZHs.Split('|');
            for (int y = 0; y < matchingZH.Length; y++)
            {
                for (int w = 0; w < DT.Length; w++)
                {
                    if (matchingZH[y].IndexOf(DT[w]) > -1)
                    {
                        index++;
                    }
                }
                if (index == DT.Length)
                {
                    for (int h = 0; h < BlueBall.Length; h++)
                    {
                        matchingZHs += matchingZH[y] + "+" + BlueBall[h] + " ";
                    }
                }
                index = 0;

            }

        }

        //string[] leng = matchingZHs.Split(' ');
        //leng.Length;
        return matchingZHs;

    }
    /// <summary>
    /// list 序列化 调用方法fnObjectToJSON(lstSix)
    ///  List<Model.LotteryAnnouncement> lstSix = new List<Model.LotteryAnnouncement>();
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="_t"></param>
    /// <returns></returns>
    public string fnObjectToJSON<T>(T _t)
    {
        JavaScriptSerializer oJavaSeria = new JavaScriptSerializer();

        try
        {
            return oJavaSeria.Serialize(_t);
        }
        catch (Exception ex)
        {

            throw new Exception("JSONHelper.ObjectToJSON(): " + ex.Message);
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}